import 'package:fish_redux/fish_redux.dart';
import 'package:flutter/cupertino.dart' hide Action;
import 'package:www.znpigai.teacher/global_store/action.dart';
import 'package:www.znpigai.teacher/global_store/state.dart';
import 'package:www.znpigai.teacher/global_store/store.dart';
import 'package:www.znpigai.teacher/utils/nav.dart';
import 'package:www.znpigai.teacher/utils/util.dart';

import 'action.dart';
import 'state.dart';

Effect<StudentMineState> buildEffect() {
  return combineEffects(<Object, Effect<StudentMineState>>{
    StudentMineAction.onAvatar: _onAvatar,
    StudentMineAction.onLogout: _onLogout,
    StudentMineAction.onClass: _onClass,
    StudentMineAction.onProfile: _onProfile,
    StudentMineAction.onAbout: _onAbout,
    StudentMineAction.onPay: _onPay,
    StudentMineAction.onMessage: _onMessage,
    StudentMineAction.onFeedback: _onFeedback,
    StudentMineAction.onReport: _onReport,
  });
}

void _onAvatar(Action action, Context<StudentMineState> ctx) async {
  var url = await Navigator.of(ctx.context).pushNamed(Nav.AVATAR_LIST);
  if (url != null) {
    var userInfo = ctx.state.userInfo;
    userInfo['headImage'] = url;
    GlobalStore.store.dispatch(GlobalActionCreator.onChangeUserInfo(userInfo));
    ctx.dispatch(StudentMineActionCreator.refresh());
  }
}

void _onLogout(Action action, Context<StudentMineState> ctx) {
  Util.showConfirmDialog('退出登录', '确定要退出登录？', ctx.context).then((ok) {
    if (ok == true) {
      GlobalState.isSignIn = false;
      Navigator.of(ctx.context).pushNamedAndRemoveUntil(
          GlobalState.isTeacher ? Nav.SOCIAL_LOGIN : Nav.LOGIN,
          (Route<dynamic> route) => false);
    }
  });
}

void _onClass(Action action, Context<StudentMineState> ctx) {
  Navigator.of(ctx.context).pushNamed(Nav.CLASS_LIST);
}

void _onProfile(Action action, Context<StudentMineState> ctx) {
  Navigator.of(ctx.context).pushNamed(Nav.PROFILE);
}

void _onAbout(Action action, Context<StudentMineState> ctx) {
  Navigator.of(ctx.context).pushNamed(Nav.ABOUT);
}

void _onPay(Action action, Context<StudentMineState> ctx) {
  Navigator.of(ctx.context).pushNamed(Nav.PAY);
}

void _onMessage(Action action, Context<StudentMineState> ctx) {
  Navigator.of(ctx.context).pushNamed(Nav.MESSAGE_LIST);
}

void _onFeedback(Action action, Context<StudentMineState> ctx) {
  Navigator.of(ctx.context).pushNamed(Nav.WEB_PAGE, arguments: {
    'url': '/correct/pub/view/feedback/index.mvc',
    'share': false,
    'done': ''
  });
}

void _onReport(Action action, Context<StudentMineState> ctx) {
  Navigator.of(ctx.context).pushNamed(Nav.WEB_PAGE, arguments: {
    'url': '/correct/student/view/study/report.mvc',
    'share': true,
    'done': ''
  });
}
